package com.lumivi.game;

import processing.core.PApplet;

import com.lumivi.devices.XIMMachine;
import com.lumivi.devices.XIMSingleScreen;

public class StressChecker1 extends XIMApplication
{
	
	int[][] targets;
	
	int currentTarget;
	float changeTime;
	
	int targetColor = this.getApplet().color(255, 255, 255);
	
	boolean playing;
	
	int mode;
	
	int points;
	
	float time; 
	
	int tempX, tempY, tempC; 
	

	public StressChecker1(PApplet applet, XIMMachine m, int[][] targets)
	{
		super(applet, m);
		this.targets = targets;
		this.initialize();
	}
	
	public StressChecker1(PApplet applet, XIMMachine m, int numTargets)
	{
		super(applet, m);
		this.generateRandomTargets(numTargets);
		this.initialize();
	}
	
	private void initialize()
	{
		this.mode = 1;
		this.points = 0;
		this.currentTarget = 0;
		this.changeTime = 8000;
		this.setPlaying(true);
	}
	
	public int getMode()
	{
		return mode;
	}
	
	private void setMode(int mode)
	{
		switch (mode)
		{
			case 1:
				this.initialize();
				break;
			case 2:
				this.setPlaying(true);
			case 3:
				break;
		}
		this.mode = mode;
	}
	
	public boolean isPlaying()
	{
		return playing;
	}

	public void setPlaying(boolean playing)
	{
		this.playing = playing;
	}

	@Override
	public void execute() 
	{   
		
		this.getScreen().getSingleScreen(0).setMainColor(applet.color(255, 0, 0)); 
		this.getScreen().getSingleScreen(0).setFadingFrequency(10); 
		
		
		this.getScreen().send(); 
		
		
		switch(mode)
		{
			case 1:
			//Waiting to start
			    if (applet.millis() - time > 1500)
			    {
			    	time = applet.millis();
			    	
					tempX = (int) applet.random(8);
					tempY = (int) applet.random(9);
					tempC = (int) applet.color(applet.random(255), applet.random(255), applet.random(255));
			    }
			    
				this.getFloor().setTile(tempX, tempY, tempC);
				
				break;
				
			case 2:
			//Playing
				if (this.isPlaying())
				{
				    if (applet.millis() - time > changeTime)
				    {
				    	time = applet.millis();
						this.nextTarget();
				    }
				    
					if (this.isTargetReached())
					{
						time = applet.millis();
						this.targetReached();
						this.nextTarget();
					}
				}

				int x = targets[currentTarget][0];
				int y = targets[currentTarget][1];
				
				this.getFloor().setTile(x, y, this.targetColor);

				break;
			
			case 3:
			//Game finished
			    if (applet.millis() - time > 1500)
			    {
			    	time = applet.millis();
			    	
					tempC = (int) applet.color(applet.random(255), applet.random(255), applet.random(255));
			    }
			    
				this.getFloor().setTiles(tempC);
				
				break;
		}
		
	}
	
	private boolean isTargetReached()
	{
//		System.out.println("Coords: " + this.getTracking().getTileX() + ", " + this.getTracking().getTileY());
//		System.out.println("Target: " + this.targets[currentTarget][0] + ", " + this.targets[currentTarget][1]);
	
		if (this.getTracking().getTileX() == this.targets[currentTarget][0] && this.getTracking().getTileY() == this.targets[currentTarget][1])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private void targetReached()
	{
		this.points++;
	}
	
	private void nextTarget()
	{
		if (this.currentTarget < this.targets.length - 1)
		{
			this.currentTarget++;
			this.changeTime -= 750;
		}
		else
		{
			this.setMode(3);
		}
	}
	
	private void generateRandomTargets(int n)
	{
		this.targets = new int[n][2];
		
		for (int i = 0; i < n - 1; i++)
		{
			this.targets[i][0] = (int) applet.random(8);
			this.targets[i][1] = (int) applet.random(9);
		}
	}
	
	
	public void keyPressed(char key)
	{
		if (key == 'r')
			this.setMode(1);
		if (key == 's')
			this.setMode(2);
	}
}
